﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            EmployeeRepo _EmployeeRepo = new EmployeeRepo();
            IEnumerable<EmployeeModels> _Employees = _EmployeeRepo.GetEmployee();
            return View(_Employees);
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(string  id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            EmployeeRepo _EmployeeRepo = new EmployeeRepo();
            EmployeeModels _Employee = new EmployeeModels();
            _Employee = _EmployeeRepo.GetEmployeeById(id);
            return View(_Employee);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            EmployeeModels _Employee = new EmployeeModels();

            DepartmentRepo _DepartmentRepo = new DepartmentRepo();
            IEnumerable<DepartmentModels> _Departments = _DepartmentRepo.GetDepartment();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Departments
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.dept_id == selectedId),
                                                         Text = s.dept_name_th,
                                                         Value = s.dept_id.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _Employee.DeptmentSelect = selectlist;
            return View(_Employee);
        } 

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                EmployeeRepo _EmployeeRepo = new EmployeeRepo();
                EmployeeModels _Employee = new EmployeeModels();

                _Employee.NameTh = collection["NameTh"];
                _Employee.NameEng = collection["NameEng"];
                _Employee.AddressTh = collection["AddressTh"];
                _Employee.AddressEng = collection["AddressEng"];
                _Employee.dept_id = SF.GetInt32(collection["dept_id"]);
                _Employee.dept_name_th = collection["dept_name_th"];
                _Employee.Tel = collection["Tel"];
                _Employee.UserName = collection["UserName"];
                _Employee.Password = collection["Password"];
                _Employee.CreateBy = (string)Session["User"];
                if (_Employee.NameTh == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูลชื่อภาษาไทย";
                    return RedirectToAction("Create");
                }
                else if (_Employee.NameEng == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อภาษาอังกฤษ";
                    return RedirectToAction("Create");
                } if (_Employee.UserName == "")
                {
                    TempData["errorname2"] = "ยังไม่ป้อนข้อมูล UserName";
                    return RedirectToAction("Create");
                }
                else if (_Employee.Password == "")
                {
                    TempData["errorname3"] = "ยังไม่ป้อนข้อมูล Password";
                    return RedirectToAction("Create");
                }
                else
                {
                    bool result = _EmployeeRepo.Create(_Employee);
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            EmployeeRepo _EmployeeRepo = new EmployeeRepo();
            EmployeeModels _Employee = new EmployeeModels();
            _Employee = _EmployeeRepo.GetEmployeeById(id);

            DepartmentRepo _DepartmentRepo = new DepartmentRepo();
            IEnumerable<DepartmentModels> _Departments = _DepartmentRepo.GetDepartment();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Departments
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.dept_id == selectedId),
                                                         Text = s.dept_name_th,
                                                         Value = s.dept_id.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _Employee.DeptmentSelect = selectlist;
            return View(_Employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                EmployeeRepo _EmployeeRepo = new EmployeeRepo();
                EmployeeModels _Employee = new EmployeeModels();
                _Employee.EmpId = id;
                _Employee.NameTh = collection["NameTh"];
                _Employee.NameEng = collection["NameEng"];
                _Employee.AddressTh = collection["AddressTh"];
                _Employee.AddressEng = collection["AddressEng"];
                _Employee.Tel = collection["Tel"];
                _Employee.dept_id = SF.GetInt32(collection["dept_id"]);
                _Employee.UserName = collection["UserName"];
                _Employee.Password = collection["Password"];
                _Employee.UpdateBy = (string)Session["User"];
                 if (_Employee.NameTh == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูลชื่อภาษาไทย";
                    return RedirectToAction("Edit");
                }
                else if (_Employee.NameEng == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อภาษาอังกฤษ";
                    return RedirectToAction("Edit");
                } if (_Employee.UserName == "")
                {
                    TempData["errorname2"] = "ยังไม่ป้อนข้อมูล UserName";
                    return RedirectToAction("Edit");
                }
                 else if (_Employee.Password == "")
                 {
                     TempData["errorname3"] = "ยังไม่ป้อนข้อมูล Password";
                     return RedirectToAction("Edit");
                 }
                 else
                 {
                     bool result = _EmployeeRepo.Edit(_Employee);

                     return RedirectToAction("Index");
                 }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Delete/5
 
        public ActionResult Delete(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            EmployeeRepo _EmployeeRepo = new EmployeeRepo();
            EmployeeModels _Employee = new EmployeeModels();
            _Employee = _EmployeeRepo.GetEmployeeById(id);
            return View(_Employee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                EmployeeRepo _EmployeeRepo = new EmployeeRepo();
                EmployeeModels _Employee = new EmployeeModels();
                _Employee.EmpId = id;
                _Employee.UpdateBy = (string)Session["User"];
                bool result = _EmployeeRepo.Delete(_Employee);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
